package com.jrx.anytxn.param.service.account.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.dto.account.SubjectDetailInfoRes;
import com.jrx.anytxn.param.entity.GaSubjectDetailInfo;
import com.jrx.anytxn.param.mapper.ext.ExtGaSubjectDetailInfoMapper;
import com.jrx.anytxn.param.service.account.ISubjectDetailInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 科目明细表 实现接口方法
 *
 * @author Qingyao Xing
 * @date 2019/9/6 13:22
 */
@Service
public class SubjectDetailInfoServiceImpl implements ISubjectDetailInfoService {
    private static final Logger logger = LoggerFactory.getLogger(SubjectDetailInfoServiceImpl.class);
    @Resource
    private ExtGaSubjectDetailInfoMapper extGaSubjectDetailInfoMapper;
    @Override
    public SubjectDetailInfoRes getModalByIdAndTenId(Long id, String tenantId) throws TxnException {
        if (id == null&& StringUtils.isBlank(tenantId)) {
            logger.error("科目明细主键ID或租户ID为空");
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        GaSubjectDetailInfo subjectDetailInfo = extGaSubjectDetailInfoMapper.selectModalByIdAndTenId(id,tenantId);
        if (null == subjectDetailInfo) {
            logger.error("根据主键[{}]查询的科目明细集合为空", id);
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        return BeanMapping.copy(subjectDetailInfo, SubjectDetailInfoRes.class);
    }

    @Override
    public TxnPage<SubjectDetailInfoRes> getPage(Integer pageNum, Integer pageSize,String tenantId,String beginDate,String endDate,String subjectId) throws TxnException {
        Page<SubjectDetailInfoRes> page = PageHelper.startPage(pageNum, pageSize);
        List<GaSubjectDetailInfo> subjectDetailInfos = extGaSubjectDetailInfoMapper.selectAllByPageAndTenId(tenantId,beginDate,endDate,subjectId);
        if (subjectDetailInfos.isEmpty()) {
            logger.error("查询的科目明细表列表为空");
            throw new TxnException(TxnRespCode.SUCCESS.getCode(), "分页查询科目明细没有数据");
        }
        List<SubjectDetailInfoRes> res = BeanMapping.copyList(subjectDetailInfos, SubjectDetailInfoRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }
}
